<?php
/**
 * Created by PhpStorm.
 * User: zc
 * Date: 2019/8/5
 * Time: 23:36
 */
namespace app\api\controller\v1;

use addons\adszone\library\Adszone;
use app\admin\controller\Zlog;
use app\common\controller\Api;
use app\common\logic\ChatManagerLogic;
use app\common\logic\ConsultM;
use app\common\validate\ConsultValidate;
use think\Db;
use think\Exception;

class Consult extends Api
{

    protected $noNeedLogin = ['index'];  //不能登陆方法
    protected $noNeedRight = '*';
    /**
     * 获取咨询室列表接口
     */
    public function index()
    {
        $page = $this->request->post('page');
        $pagesize = $this->request->post('pagesize');
        $model = new ConsultM();
        $data = $model->setOrgCateId(ORG_CATE_ID)->setPage($page)->setPagesize($pagesize)->getList();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }
    public function getBanner() {
        //获取首页轮播图
        $pic = new Adszone('special_img');
        $list = $pic->getAdsByMark('special_img',ORG_CATE_ID);
        $this->success('轮播图', $list);
    }

    /**
     * 获取详情
     */
    public function detail()
    {
        $id = $this->request->post('id');
        $model = new ConsultM();
        $data = $model->setId($id)->getDetail();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }

    /*
     * 提交咨询
     */
    public function doConsult()
    {
        //数据验证
        (new ConsultValidate())->goCheck('consult');
        $model = new ConsultM();
        $res = $model->setUserId(UID)->setOrgCateId(ORG_CATE_ID)->saveData($this->request->param());
        if($res){
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }

    }

    public function consultStatus()
    {
        $id = $this->request->post('id');
        if($id <= 0){
            $this->error('缺少咨询订单id！');
        }
        $data = Db::name('consult_reply')->where(['id'=> $id])->find();
        $this->success('咨询是否完成！',$data);
    }

    /*
     * 关闭咨询室
     */
    public function overConsult()
    {
        $room_id = $this->request->post('room_id');
        if(empty($room_id)){
            $this->error('缺少咨询室id！');
        }
        $chat = new ChatManagerLogic();
        $res = $chat->close($room_id);
        if($res){
            Db::name('consult_reply')->where(['room_id'=> $room_id])->update(['type'=>4,'ent_time'=>time()]);
        }
        if($res){
            $this->success('操作成功！');
        }else{
            $this->error('操作失败！');
        }
    }
}